//
//  QYGGameCompleteTipView.m
//  wawaji_ios
//
//  Created by 云+物娱 on 2017/12/7.
//  Copyright © 2017年 yiguo qi. All rights reserved.
//

#import "QYGGameCompleteTipView.h"
#import "NSTimer+Utility.h"

@interface QYGGameCompleteTipView()

@property (nonatomic, strong) UIImageView *headerView;
@property (nonatomic, strong) UILabel *detailLabel;
@property (nonatomic, strong) UIImageView *imgView;
@property (nonatomic, strong) UIButton *cancelBtn;
@property (nonatomic, strong) UIButton *sureBtn;
@property (nonatomic, strong) NSTimer *timer;
// 倒计时
@property (nonatomic, assign) NSInteger countDown;

@end



@implementation QYGGameCompleteTipView

- (void)dealloc{
    
    NSLog(@"----QYGGameCompleteTipView----");
    if(self.timer){
        [self.timer invalidate];
        self.timer = nil;
    }
}
- (void)setDrollState:(GameCompleteState)drollState{
    
    _drollState = drollState;
    NSString *detailStr;
    NSString *cancelStr;
    NSString *sureStr;
    NSString *img;
    NSString *headImg;
    if(drollState == GameCompleteStateFail){
        detailStr = @"就差一点点，不要放弃！";
        cancelStr = @"休息一下";
        sureStr = @"再来一次";
        img = @"droll_tip_fail_img";
        headImg = @"droll_tip_fail_head";
    }else if(drollState == GameCompleteStateSucceed){
        
        detailStr = @"获得娃娃一个，是否继续？";
        cancelStr = @"休息一下";
        sureStr = @"再来一次";
        img = @"droll_tip_sucs_img";
        headImg = @"droll_tip_sud_head";
    }else if(drollState == GameCompleteStateBalanceLack){
        
        detailStr = @"金币不足，是否充值再战？";
        cancelStr = @"稍后再试";
        sureStr = @"立即充值";
        img = @"droll_tip_fail_img";
        headImg = @"droll_tip_fail_head";
//        appendStr = nil;
        [self.sureBtn setTitle:sureStr forState:UIControlStateNormal];
    }
    
    self.imgView.image = [UIImage imageNamed:img];
    self.detailLabel.text = detailStr;
    [self.cancelBtn setTitle:cancelStr forState:UIControlStateNormal];
    self.headerView.image = [UIImage imageNamed:headImg];
    
    [self startTimerWithState:drollState];
    
}
- (void)startTimerWithState:(GameCompleteState)drollState{
    
    if(drollState == GameCompleteStateBalanceLack) return;
    self.countDown = 9;
    WS(weakSelf);
    self.timer = [NSTimer cm_scheduledTimerWithTimeInterVal:1 block:^{
        [weakSelf showCountTime];
    } repeats:YES];
    [self.timer fire];
}
- (void)showCountTime{
    
    if (self.countDown < 0) {
        [self.timer invalidate];
        self.timer = nil;
        [self sureBtnClick];
    }

    
    NSString *sureStr = @"再来一次";
    NSString *appendStr = [NSString stringWithFormat:@"(%ld)",self.countDown];
    NSMutableAttributedString *AttributedStr = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@%@",sureStr,appendStr]];
    
    [AttributedStr addAttribute:NSForegroundColorAttributeName
     
                          value:[UIColor lightGrayColor]
     
                          range:NSMakeRange(sureStr.length, appendStr.length)];
    
    [self.sureBtn setAttributedTitle:AttributedStr forState:UIControlStateNormal];
    self.countDown--;
}

#pragma mark target
- (void)cancelBtnClick{
    if(self.timer){
        [self.timer invalidate];
        self.timer = nil;
    }
    if(self.cancelBlock){
        self.cancelBlock(self.drollState);
    }
}

- (void)sureBtnClick{
    if(self.timer){
        [self.timer invalidate];
        self.timer = nil;
    }
    if(self.suerBlock){
        self.suerBlock(self.drollState);
    }
}
- (instancetype)initWithFrame:(CGRect)frame{
    
    self = [super initWithFrame:frame];
    if(self){
        
        self.backgroundColor = [UIColor cm_mainColor];
        self.layer.cornerRadius = 10;
//        self.layer.masksToBounds = YES;
        
        [self initUI];
    }
    return self;
    
}

#pragma mark setUI
- (void)initUI{
    
    [self addSubview:self.headerView];
    [self addSubview:self.detailLabel];
    [self addSubview:self.imgView];
    [self addSubview:self.cancelBtn];
    [self addSubview:self.sureBtn];
    
    
    [self.headerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(@47);
        make.width.equalTo(@128);
        make.top.equalTo(self.mas_top).offset(-24);
        make.centerX.equalTo(self.mas_centerX);
    }];
    
    [self.imgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.mas_top).offset(ScaleW(50));
        make.centerX.equalTo(self.mas_centerX);
        make.width.equalTo(@(ScaleW(165)));
        make.height.equalTo(@(ScaleW(165)));
    }];
    
    [self.cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(@(ScaleW(49)));
        make.width.equalTo(@(ScaleW(180)));
        make.bottom.equalTo(self.mas_bottom).offset(-ScaleW(30));
        make.centerX.equalTo(self.mas_centerX);
    }];

    [self.sureBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(@(ScaleW(49)));
        make.width.equalTo(@(ScaleW(180)));
        make.bottom.equalTo(self.cancelBtn.mas_top).offset(-ScaleW(16));
        make.centerX.equalTo(self.cancelBtn.mas_centerX);
    }];
    
    [self.detailLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.sureBtn.mas_top).offset(-ScaleW(20));
        make.left.equalTo(self.mas_left);
        make.right.equalTo(self.mas_right);
        make.height.equalTo(@(ScaleH(30)));
    }];
    
    
    
}


- (UIImageView *)imgView{
    if(!_imgView){
        _imgView = [[UIImageView alloc] init];
    }
    return _imgView;
}

- (UIButton *)sureBtn{
    
    if(!_sureBtn){
        _sureBtn = [UIButton buttonWithBackgroundImageName:@"droll_tip_sure"];;
        [_sureBtn addTarget:self action:@selector(sureBtnClick) forControlEvents:UIControlEventTouchUpInside];
        [_sureBtn setTitleColor:[UIColor colorWithHex:@"6b0000"] forState:UIControlStateNormal];
    }
    return _sureBtn;
}

- (UIButton *)cancelBtn{
    
    if(!_cancelBtn){
        _cancelBtn = [UIButton buttonWithBackgroundImageName:@"droll_tip_cancel"];
        [_cancelBtn addTarget:self action:@selector(cancelBtnClick) forControlEvents:UIControlEventTouchUpInside];
        [_cancelBtn setTitleColor:[UIColor colorWithHex:@"6b0000"] forState:UIControlStateNormal];
    }
    return _cancelBtn;
}

- (UILabel *)detailLabel{
    if(!_detailLabel){
        _detailLabel = [UILabel labelWithText:@"" setFont:ScaleFont(16) setTextColor:[UIColor colorWithHex:@"FFFEFE"] setTextAlignment:NSTextAlignmentCenter];
    }
    return _detailLabel;
}


- (UIImageView *)headerView{
    if(!_headerView){
        _headerView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"droll_tip_sud_head"]];
    }
    return _headerView;
}

@end
